import Api, { GET_ORDER_DETAIL, CONFIRM_ORDER, CANCEL_ORDER, PAY_ORDER} from '../../../utils/api.js';
import { handPic, getUrl } from '../../../utils/util.js';


const App = getApp();

Page({
    data: {
        ready: false,       // 页面准备完成?
    },
    onLoad: function (options) {
        // this.count_down();
        let _this = this,
            key;


        //判断是否登录
        if (Api.checkLogin(function () {
            _this.onLoad(options);
        })) {
            key = Api.getKey();
            //获取用户数据接口
            _this.setData({
                key: key
            })
        }


        //获取订单详情
        Api.fetch(GET_ORDER_DETAIL, { key: key, order_sn: options.order_sn }, resp => {
            resp.goods_list.map((v, k) => {
                v.goods_img = handPic(v.goods_img, 4);
            })
            _this.setData({
                info: resp,
                ready: true
            })
        })
    },


    /** cancel 取消订单 */
    cancelOrder: function () {
        let { key, info } = this.data,
            _this = this;
            
        wx.showModal({
            title: '取消订单',
            content: '确认取消订单吗?',
            success: function (res) {
                if (res.confirm) {
                    Api.fetch(CANCEL_ORDER, { key: key, ordersn:info.order_sn }, resp => {
                        wx.redirectTo({
                            url: '/pages/mine/order_list/order_list?type=0',
                        })
                    })
                }
            }
        })
    },
    /** 提醒发货*/
    remindHandle: function () {
       
    },

    //支付订单
    dopay: function () {
        let { key,info } = this.data,
            _this = this;
        wx.showLoading({
            title: '支付中',
        })
        Api.fetch(PAY_ORDER, { key: key, order_sn: info.order_sn }, resp => {
            wx.requestPayment({
                'timeStamp': resp.timeStamp,
                'nonceStr': resp.nonceStr,
                'package': resp.package,
                'signType': 'MD5',
                'paySign': resp.paySign,
                'success': function (res) {
                    wx.hideLoading();
                    info.order_state = 2;
                    _this.setData({
                        info:info
                    })
                },
                'fail': function (res) {
                    wx.hideLoading();
                    wx.showToast({
                        title: '支付取消',
                    })
                }
            })
        })
    },

    /** 确认收货 /删除订单*/
    confirmOrder: function () {
        let { key, info } = this.data,
            _this = this;
        wx.showModal({
            title: '我要确认收货',
            success: function (res) {
                if (res.confirm) {
                    Api.fetch(CONFIRM_ORDER, { key: key,orderid:info.order_id }, resp => {
                        info.order_state = 4;
                        _this.setData({
                            info:info
                        })
                        wx.showToast({
                            title: '操作成功',
                        })
                    })
                }
            }
        })
    },
    /** 普通跳转 */
    navigateTo: function (e) {

        let { tip,idx } = e.currentTarget.dataset,
            url;

        url = getUrl(tip,{gc_id:idx});
        
        App.navigateTo(url);
        
    },


    /** 页面准备完成 */
    onReady: function () {

    },
    /** 页面显示 */
    onShow: function () {

    },
    /** 分享转发 */
    onShareAppMessage: function () {

    }
})